package com.bsj.power.pls.handle;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bsj.power.common.config.cached.*;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.dto.cardReport.BindCardDTO;
import com.bsj.power.common.def.entity.job.WorkPersonnel;
import com.bsj.power.common.def.entity.locationDevice.LocationDevice;
import com.bsj.power.common.def.entity.personnel.Personnel;
import com.bsj.power.common.def.mq.sdk.face.PersonInfo;
import com.bsj.power.common.def.vo.PersonnelInfoVO;
import com.bsj.power.common.mapper.PersonnelMapper;
import com.bsj.power.common.mapper.WorkPersonnelMapper;
import com.bsj.power.common.netty.*;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.common.util.ExceptionUtil;
import com.bsj.power.pls.cache.BindCardMachineCache;
import com.bsj.power.pls.service.LocationDeviceService;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.hikvision.artemis.sdk.constant.ContentType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 将网关解析的数据分发给对应的业务
 */
@Slf4j
@Component
public class CTDataHandle {

    @Value("${hiKv.host}")
    private String host;

    @Value("${hiKv.appKey}")
    private String appKey;

    @Value("${hiKv.appSecret}")
    private String appSecret;

    /**
     * url格式：https://{hostname}:{port}/artemis/{uri}。
     */
    private final String ARTEMIS_PATH = "/artemis";

    @Autowired
    private BindCardCached bindCardCached;

    @Autowired
    private LocationDeviceService locationDeviceService;

    @Autowired
    private RedisCached redisCached;

    @Resource
    private PersonnelMapper personnelMapper;

    @Resource
    private WorkPersonnelMapper workPersonnelMapper;

    @Autowired
    private PersonnelCached personnelCached;

    @Autowired
    private WorkPersonnelCached workPersonnelCached;

    @Autowired
    private LocationDeviceCached locationDeviceCached;

    /**
     * 数据分发给对应的业务处理
     *
     * @param ctData
     */
    public void handleData(CTData ctData) {
        try {
            String dataType = ctData.getDataType();
            switch (dataType) {
                //设备状态上报
                case CT60_constant.AC:
                    ACDeviceStatusData acDeviceStatusData = (ACDeviceStatusData) ctData;
                    BindCardMachineCache.putAcDeviceStatusData(acDeviceStatusData);
                    BindCardMachineCache.putStockInfoList(acDeviceStatusData);
                    log.info("接收到设备状态信息上报：{}", acDeviceStatusData.toString());
                    break;
                //工牌归还上报
                case CT60_constant.CR:
                    CRReceiveData crReceiveData = (CRReceiveData) ctData;
                    log.info("接收到工牌归还上报：{}", crReceiveData.toString());
                    crDataHandle(crReceiveData);
                    break;
                //工牌弹出上报
                case CT60_constant.CP:
                    CPPopData cpPopData = (CPPopData) ctData;
                    log.info("接收到工牌弹出上报：{}", cpPopData.toString());
                    cpDataHandle(cpPopData);
                    break;
                case CT60_constant.CL:
                    CLStockControlData clStockControlData = (CLStockControlData) ctData;
                    log.info("接收到仓位启用或禁用命令应答：{}", clStockControlData.toString());
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("CTDataHandle异常：", ExceptionUtil.getStackStr(e));
        }
    }

    /**
     * 工牌弹出信息上报处理
     *
     * @param cpPopData
     * @return void
     * @author ljx
     * @time 2024/9/29 16:10
     */
    private void cpDataHandle(CPPopData cpPopData) {
        Integer result = cpPopData.getResult();
        if (result != null && result != NumericalConstants.ONE) {
            log.info("工牌弹出信息上报处理，绑定失败，上报信息：{}", cpPopData);
            return;
        }
        Integer serialNum = cpPopData.getSerialNum();
        String deviceId = cpPopData.getDeviceId();
        String uwbId = cpPopData.getUwbId();
        if (serialNum == null || StringUtils.isAnyBlank(deviceId, uwbId)) {
            log.info("工牌弹出信息上报处理，绑定失败，仓位号或弹卡几编号或设备号为空，上报信息：{}", cpPopData);
            return;
        }
        //将该仓位置空
        BindCardMachineCache.putStockInfo(deviceId, new StockInfo(serialNum));
        //获取设备号与人员编号的关系
        String personInfoStr = redisCached.get(RedisConstant.DEVICE_NAME_EXT_EVENT_PERSON_NO_BIND_KEY + uwbId);
        log.info("获取设备号与人员的关系，设备号：{}，人员信息：{}", uwbId, personInfoStr);
        if (StringUtils.isBlank(personInfoStr)) {
            log.info("设备号与人员的关系为空，设备号：{}", uwbId);
            return;
        }
        PersonInfo personInfo = JSONObject.parseObject(personInfoStr, PersonInfo.class);
        if (personInfo != null) {
            String name = personInfo.getName();
            String extEventPersonNo = personInfo.getExtEventPersonNo();
            if (StringUtils.isBlank(extEventPersonNo)) {
                log.info("人员信息有问题，人员编号为空，姓名为：{}", name);
                return;
            }
            //同名数量，负责人与作业人员都参与统计
            int size = NumericalConstants.ZERO;
            WorkPersonnel newWorkPersonnel = null;
            //根据人员编号查询负责人信息与作业人员信息，看平台内是否有对应的人
            Personnel personnel = personnelMapper.selectOne(Wrappers.<Personnel>lambdaQuery().eq(Personnel::getExtEventPersonNo, extEventPersonNo));
            WorkPersonnel workPersonnel = workPersonnelMapper.selectOne(Wrappers.<WorkPersonnel>lambdaQuery().eq(WorkPersonnel::getExtEventPersonNo, extEventPersonNo));
            //如果没有这个人员则根据名字查询系统内是否已存在，如果存在一个则把人员编号插入，如果存在多个则选一个插入人员编号并给出同名的提示，如果没有则添加新人员
            if (personnel == null && workPersonnel == null) {
                if (StringUtils.isBlank(name)) {
                    log.info("人员信息有问题，姓名为空，人员编号为：{}", extEventPersonNo);
                    return;
                }
                List<Personnel> personnelList = personnelMapper.selectList(Wrappers.<Personnel>lambdaQuery().eq(Personnel::getName, name));
                List<WorkPersonnel> workPersonnelList = workPersonnelMapper.selectList(Wrappers.<WorkPersonnel>lambdaQuery().eq(WorkPersonnel::getName, name));
                if (!CollectionUtils.isEmpty(personnelList)) {
                    size = personnelList.size();
                    personnel = getNotBindingPersonnel(personnelList);
                    //负责人内无未绑卡的人员
                    if (personnel == null) {
                        if (!CollectionUtils.isEmpty(workPersonnelList)) {
                            size += workPersonnelList.size();
                            workPersonnel = getNotBindingWorkPersonnel(workPersonnelList);
                            //作业人员内无未绑卡的人员
                            if (workPersonnel == null) {
                                //添加新的同名之人
                                newWorkPersonnel = insertWorkPersonnel(extEventPersonNo, name);
                                size += NumericalConstants.ONE;
                            } else {
                                workPersonnelMapper.updateById(new WorkPersonnel(workPersonnel.getWorkPersonnelId(), extEventPersonNo));
                            }
                        }
                    } else {
                        if (!CollectionUtils.isEmpty(workPersonnelList)) {
                            size += workPersonnelList.size();
                        }
                        personnelMapper.updateById(new Personnel(personnel.getPersonnelId(), extEventPersonNo));
                    }
                } else if (!CollectionUtils.isEmpty(workPersonnelList)) {
                    size = workPersonnelList.size();
                    //TODO 同名的是否需要判断第一个人是否已经绑卡，如果绑了是否需要依次往后选人绑定
                    workPersonnel = getNotBindingWorkPersonnel(workPersonnelList);
                    //作业人员内无未绑卡的人员
                    if (workPersonnel == null) {
                        //添加新的同名之人
                        newWorkPersonnel = insertWorkPersonnel(extEventPersonNo, name);
                        size += NumericalConstants.ONE;
                    } else {
                        workPersonnelMapper.updateById(new WorkPersonnel(workPersonnel.getWorkPersonnelId(), extEventPersonNo));
                    }
                } else {
                    size = NumericalConstants.ONE;
                    //添加新作业人员
                    newWorkPersonnel = insertWorkPersonnel(extEventPersonNo, name);
                }
                if (personnel == null && workPersonnel == null) {
                    workPersonnel = newWorkPersonnel;
                }
            } else {
                if (StringUtils.isNotBlank(name)) {
                    long personnelNumber = personnelMapper.selectCount(Wrappers.<Personnel>lambdaQuery().eq(Personnel::getName, name));
                    long workPersonnelNumber = workPersonnelMapper.selectCount(Wrappers.<WorkPersonnel>lambdaQuery().eq(WorkPersonnel::getName, name));
                    size = (int) (personnelNumber + workPersonnelNumber);
                }
            }
            //工号
            String empNo;
            //姓名
            String personnelName;
            //身份证号
            String idCardNo;
            //人员类型
            Integer classify;
            if (personnel != null) {
                empNo = personnel.getEmpNo();
                personnelName = personnel.getName();
                idCardNo = personnel.getIdCardNo();
                classify = NumericalConstants.ZERO;
            } else {
                empNo = workPersonnel.getTemporaryEmpNo();
                personnelName = workPersonnel.getName();
                idCardNo = workPersonnel.getIdCardNo();
                classify = NumericalConstants.ONE;
            }
            BindCardDTO oldBindCardDTO = bindCardCached.hgetBindCardInfo(RedisConstant.BIND_CARD_REDIS_EMP_NO_KEY + empNo);
            if (oldBindCardDTO == null) {
                //有多个同名的人，需要警告提示
                Integer isRepetitionName = null;
                if (size > NumericalConstants.ONE) {
                    isRepetitionName = NumericalConstants.ONE;
                }
                //绑卡操作
                BindCardDTO bindCardDTO = new BindCardDTO(uwbId, classify, personnelName, empNo, idCardNo, NumericalConstants.ONE, extEventPersonNo);
                JsonResult jsonResult = locationDeviceService.isBind(bindCardDTO, isRepetitionName);
                if (jsonResult.getCode() == 200) {
                    //删除设备号与人员的关系
                    redisCached.del(RedisConstant.DEVICE_NAME_EXT_EVENT_PERSON_NO_BIND_KEY + uwbId);
                }
                log.info("绑卡结果：{}", jsonResult);
            } else {
                //如果绑卡信息存在则将人员编号填充进绑卡信息内，待退卡时删除人员编号缓存
                oldBindCardDTO.setExtEventPersonNo(extEventPersonNo);
                bindCardCached.hsetBindCardInfo(oldBindCardDTO);
                //记录平台手动绑卡后，仍然扫脸弹卡的人
                LocationDevice locationDevice = locationDeviceCached.hgetLocationDeviceInfo(uwbId);
                if (locationDevice != null) {
                    locationDevice.setPersonName(personnelName);
                    locationDeviceCached.hsetLocationDeviceInfo(locationDevice);
                }
//                        if (classify.equals(NumericalConstants.ZERO)) {
//                            Personnel oldPersonnel = personnelCached.hgetPersonnelInfo(empNo);
//                            if (oldPersonnel != null) {
//                                oldPersonnel.setDeviceName(uwbId);
//                                personnelCached.hsetPersonnelInfo(personnel);
//                            }
//                        } else {
//                            WorkPersonnel oldWorkPersonnel = workPersonnelCached.getWorkPersonnelInfo(empNo);
//                            if (oldWorkPersonnel != null) {
//                                oldWorkPersonnel.setDeviceName(uwbId);
//                                workPersonnelCached.setWorkPersonnelInfo(workPersonnel);
//                            }
//                        }
                log.info(personnelName + "已绑卡，人脸识别智慧绑卡失败，绑卡记录未添加");
            }
        } else {
            log.info("根据设备号获取人员信息为空，设备号：{}", uwbId);
        }
    }

    /**
     * 添加作业人员
     *
     * @param extEventPersonNo
     * @param name
     * @return com.bsj.power.common.def.entity.job.WorkPersonnel
     * @author ljx
     * @time 2025/1/4 10:54
     */
    private WorkPersonnel insertWorkPersonnel(String extEventPersonNo, String name) {
        WorkPersonnel newWorkPersonnel = new WorkPersonnel(extEventPersonNo, name, IdUtil.objectId());
        workPersonnelMapper.insert(newWorkPersonnel);
        workPersonnelCached.setWorkPersonnelInfo(newWorkPersonnel);
        return newWorkPersonnel;
    }

    /**
     * 获取为绑卡并且人员编号为空的作业人员
     *
     * @param workPersonnelList
     * @return com.bsj.power.common.def.entity.job.WorkPersonnel
     * @author ljx
     * @time 2025/1/4 10:16
     */
    private WorkPersonnel getNotBindingWorkPersonnel(List<WorkPersonnel> workPersonnelList) {
        for (WorkPersonnel workPersonnel : workPersonnelList) {
            //已绑卡
            if (bindCardCached.hgetBindCardInfo(RedisConstant.BIND_CARD_REDIS_EMP_NO_KEY + workPersonnel.getTemporaryEmpNo()) != null) {
                continue;
            }
            //人员编号不为空
            if (StringUtils.isNotBlank(workPersonnel.getExtEventPersonNo())) {
                continue;
            }
            return workPersonnel;
        }
        return null;
    }

    /**
     * 获取未绑卡并且人员编号为空的负责人
     *
     * @param personnelList
     * @return com.bsj.power.common.def.entity.personnel.Personnel
     * @author ljx
     * @time 2025/1/4 10:03
     */
    private Personnel getNotBindingPersonnel(List<Personnel> personnelList) {
        for (Personnel personnel : personnelList) {
            //已绑卡
            if (bindCardCached.hgetBindCardInfo(RedisConstant.BIND_CARD_REDIS_EMP_NO_KEY + personnel.getEmpNo()) != null) {
                continue;
            }
            //人员编号不为空
            if (StringUtils.isNotBlank(personnel.getExtEventPersonNo())) {
                continue;
            }
            return personnel;
        }
        return null;
    }

//    /**
//     * 工牌弹出信息上报处理
//     *
//     * @param cpPopData
//     * @return void
//     * @author ljx
//     * @time 2024/9/29 16:10
//     */
//    private void cpDataHandle(CPPopData cpPopData) {
//        Integer result = cpPopData.getResult();
//        if (result != null && result != NumericalConstants.ONE) {
//            log.info("工牌弹出信息上报处理，绑定失败，上报信息：{}", cpPopData);
//            return;
//        }
//        Integer serialNum = cpPopData.getSerialNum();
//        String deviceId = cpPopData.getDeviceId();
//        String uwbId = cpPopData.getUwbId();
//        if (serialNum == null || StringUtils.isAnyBlank(deviceId, uwbId)) {
//            log.info("工牌弹出信息上报处理，绑定失败，仓位号或弹卡几编号或设备号为空，上报信息：{}", cpPopData);
//            return;
//        }
//        //获取设备号与人员编号的关系
//        String extEventPersonNo = redisCached.get(RedisConstant.DEVICE_NAME_EXT_EVENT_PERSON_NO_BIND_KEY + uwbId);
//        log.info("获取设备号与人员编号的关系，设备号：{}，人员编号：{}", uwbId, extEventPersonNo);
//        if (StringUtils.isNotBlank(extEventPersonNo)) {
//            //不使用JSON转的话会多出双引号
//            extEventPersonNo = JSONObject.parseObject(extEventPersonNo, String.class);
//            //根据人员编号查询负责人信息与作业人员信息，看平台内是否有对应的人
//            Personnel personnel = personnelMapper.selectOne(Wrappers.<Personnel>lambdaQuery().eq(Personnel::getExtEventPersonNo, extEventPersonNo));
//            WorkPersonnel workPersonnel = workPersonnelMapper.selectOne(Wrappers.<WorkPersonnel>lambdaQuery().eq(WorkPersonnel::getExtEventPersonNo, extEventPersonNo));
//            //如果没有这个人员则调用第三方api查询人员信息，根据身份证号判断数据库是否有这个人，如果有则修改人员id，如果没有添加这个人员信息
//            if (personnel == null && workPersonnel == null) {
//                WorkPersonnel newWorkPersonnel = getPersonInfoIdCardNo(extEventPersonNo);
//                if (newWorkPersonnel == null || StringUtils.isBlank(newWorkPersonnel.getIdCardNo())) {
//                    log.info("综合安防平台未查询到对应的人员信息或该人员身份证不存在，人员编号为：{}", extEventPersonNo);
//                    return;
//                }
//                // 人员信息赋值操作，不能直接操作personnel与workPersonnel对象
//                // 经过验证即使在insertOrUpdateWorkPersonnel中对personnel或workPersonnel本身赋值，等方法结束后personnel与workPersonnel依然为空
//                PersonnelInfoVO personnelInfoVO = new PersonnelInfoVO(personnel, workPersonnel);
//                //添加作业人员信息或填充人员编号，此方法执行后三个信息至少有一个不为空
//                insertOrUpdateWorkPersonnel(newWorkPersonnel, personnelInfoVO);
//                personnel = personnelInfoVO.getPersonnel();
//                workPersonnel = personnelInfoVO.getWorkPersonnel();
//                if (personnel == null && workPersonnel == null) {
//                    workPersonnel = newWorkPersonnel;
//                }
//            }
//            //工号
//            String empNo;
//            //姓名
//            String name;
//            //身份证号
//            String idCardNo;
//            //人员类型
//            Integer classify;
//            if (personnel != null) {
//                empNo = personnel.getEmpNo();
//                name = personnel.getName();
//                idCardNo = personnel.getIdCardNo();
//                classify = NumericalConstants.ZERO;
//            } else {
//                empNo = workPersonnel.getTemporaryEmpNo();
//                name = workPersonnel.getName();
//                idCardNo = workPersonnel.getIdCardNo();
//                classify = NumericalConstants.ONE;
//            }
//            BindCardDTO oldBindCardDTO = bindCardCached.hgetBindCardInfo(RedisConstant.BIND_CARD_REDIS_EMP_NO_KEY + empNo);
//            if (oldBindCardDTO == null) {
//                //绑卡操作
//                BindCardDTO bindCardDTO = new BindCardDTO(uwbId, classify, name, empNo, idCardNo, NumericalConstants.ONE, extEventPersonNo);
//                JsonResult jsonResult = locationDeviceService.isBind(bindCardDTO);
//                if (jsonResult.getCode() == 200) {
//                    //删除设备号与人员编号的关系
//                    redisCached.del(RedisConstant.DEVICE_NAME_EXT_EVENT_PERSON_NO_BIND_KEY + uwbId);
//                }
//                log.info("绑卡结果：{}", jsonResult);
//            } else {
//                log.info(name + "已绑卡，人脸识别智慧绑卡失败，绑卡记录未添加");
//            }
//        } else {
//            log.info("根据设备号获取人员编号为空，设备号：{}", uwbId);
//        }
//    }

//    /**
//     * 工牌弹出信息上报处理
//     *
//     * @param cpPopData
//     * @return void
//     * @author ljx
//     * @time 2024/9/29 16:10
//     */
//    private void cpDataHandle(CPPopData cpPopData) {
//        Integer result = cpPopData.getResult();
//        if (result != null && result != NumericalConstants.ONE) {
//            log.info("工牌弹出信息上报处理，绑定失败，上报信息：{}", cpPopData);
//            return;
//        }
//        Integer serialNum = cpPopData.getSerialNum();
//        String deviceId = cpPopData.getDeviceId();
//        if (serialNum == null || StringUtils.isBlank(deviceId)) {
//            log.info("工牌弹出信息上报处理，绑定失败，仓位号或弹卡几编号为空，上报信息：{}", cpPopData);
//            return;
//        }
//        String bindCardDTOStr = redisCached.hget(RedisConstant.TEMP_BIND_CARD_REDIS_KEY + deviceId, serialNum + "");
//        String stockInfoStr = redisCached.hget(RedisConstant.TEMP_STOCK_REDIS_KEY + deviceId, serialNum + "");
//        if (StringUtils.isAnyBlank(bindCardDTOStr, stockInfoStr)) {
//            log.info("工牌弹出信息上报处理，绑定失败，临时缓存信息为空，临时绑卡信息：{}，临时仓位信息：{}", bindCardDTOStr, stockInfoStr);
//            return;
//        }
//        BindCardDTO bindCardDTO = JSONObject.parseObject(bindCardDTOStr, BindCardDTO.class);
//        StockInfo stockInfo = JSONObject.parseObject(stockInfoStr, StockInfo.class);
//        if (bindCardDTO != null && stockInfo != null) {
//            JsonResult jsonResult = locationDeviceService.isBind(bindCardDTO);
//            redisCached.hdel(RedisConstant.TEMP_BIND_CARD_REDIS_KEY + deviceId, serialNum + "");
//            redisCached.hdel(RedisConstant.TEMP_STOCK_REDIS_KEY + deviceId, serialNum + "");
//            String extEventPersonNo = bindCardDTO.getExtEventPersonNo();
//            if (StringUtils.isNotBlank(extEventPersonNo)) {
//                redisCached.del(RedisConstant.EXT_EVENT_PERSON_NO_KEY + extEventPersonNo);
//                log.info("绑卡后删除人员编号缓存成功：{}", extEventPersonNo);
//            }
//            //将仓位设置为空仓
////            stockInfo.setState(NumericalConstants.ZERO);
////            stockInfo.setUwbNo(null);
////            stockInfo.setUwbPower(null);
////            stockInfo.setUwbVersion(null);
////            BindCardMachineCache.putStockInfo(deviceId, stockInfo);
//            log.info("绑卡结果：{}", jsonResult);
//        } else {
//            log.info("工牌弹出信息上报处理，绑定失败，临时缓存信息为空，临时绑卡信息：{}，临时仓位信息：{}", bindCardDTO, stockInfo);
//        }
//    }

    /**
     * 工牌归还信息上报处理
     *
     * @param crReceiveData
     * @return void
     * @author ljx
     * @time 2024/9/24 19:14
     */
    private void crDataHandle(CRReceiveData crReceiveData) {
        Integer result = crReceiveData.getResult();
        if (result != null && result != NumericalConstants.ONE) {
            log.info("工牌归还信息上报处理，解绑失败，上报信息：{}", crReceiveData);
            return;
        }
        //解绑操作
        String uwbId = crReceiveData.getUwbId();
        if (StringUtils.isNotBlank(uwbId)) {
            BindCardDTO bindCardDTO = bindCardCached.hgetBindCardInfo(RedisConstant.BIND_CARD_REDIS_DEVICE_NAME_KEY + uwbId);
            if (bindCardDTO != null) {
                bindCardDTO.setRemark("无需扫脸自动归还");
                bindCardDTO.setType(NumericalConstants.TWO);
                bindCardDTO.setBindWay(NumericalConstants.TWO);
                JsonResult jsonResult = locationDeviceService.isBind(bindCardDTO, null);
                log.info("工牌归还信息上报处理，解绑结果：{}", jsonResult);
            } else {
                //清除已经在平台手动绑卡后，仍然扫脸弹卡的相关信息
                LocationDevice locationDevice = locationDeviceCached.hgetLocationDeviceInfo(uwbId);
                if (locationDevice != null) {
                    locationDevice.setPersonName(null);
                    locationDeviceCached.hsetLocationDeviceInfo(locationDevice);
                }
            }
            //改变绑卡机仓位状态
            StockInfo stockInfo = new StockInfo(crReceiveData);
            BindCardMachineCache.putStockInfo(crReceiveData.getDeviceId(), stockInfo);
        }
    }

    /**
     * 根据人员id获取人员身份证号
     * 请求海康“根据人员唯一字段获取人员详细信息”API
     *
     * @param extEventPersonNo
     * @return java.lang.String
     * @author ljx
     * @time 2024/9/25 11:43
     */
    private WorkPersonnel getPersonInfoIdCardNo(String extEventPersonNo) {
        ArtemisConfig.host = host;
        ArtemisConfig.appKey = appKey;
        ArtemisConfig.appSecret = appSecret;
        // 调用接口地址
        final String url = "/api/resource/v1/person/condition/personInfo";
        final String httpStr = "https://";
        Map<String, String> path = Map.of(httpStr, ARTEMIS_PATH + url);

        //根据人员唯一字段获取人员详细信息请求参数
        String[] array = {extEventPersonNo};
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("paramName", "personId");
        jsonBody.put("paramValue", array);
        String body = jsonBody.toString();
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, ContentType.CONTENT_TYPE_JSON);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            Integer code = jsonObject.getInteger("code");
            if (code != null && code.equals(NumericalConstants.ZERO)) {
                log.info("请求根据人员唯一字段获取人员详细信息成功，请求地址：{}，请求参数{}，返回内容：{}", JSONObject.toJSONString(path), body, result);
                JSONObject data = jsonObject.getJSONObject("data");
                if (data == null) {
                    return null;
                }
                JSONArray jsonArray = data.getJSONArray("list");
                if (CollectionUtils.isEmpty(jsonArray)) {
                    return null;
                }
                List<JSONObject> jsonObjects = jsonArray.toJavaList(JSONObject.class);
                if (CollectionUtils.isEmpty(jsonObjects)) {
                    return null;
                }
                //人员信息
                JSONObject personInfo = jsonObjects.get(NumericalConstants.ZERO);
                //证件类型 111代表身份证
                Integer certificateType = personInfo.getInteger("certificateType");
                //证件号码
                String certificateNo = personInfo.getString("certificateNo");
                if (certificateType == null || certificateType != 111 || StringUtils.isBlank(certificateNo)) {
                    return null;
                }
                //姓名
                String personName = personInfo.getString("personName");
                //电话
                String phoneNo = personInfo.getString("phoneNo");
                WorkPersonnel workPersonnel = new WorkPersonnel(personName, phoneNo, certificateNo, extEventPersonNo);
                return workPersonnel;
            } else {
                log.info("请求根据人员唯一字段获取人员详细信息失败，请求地址：{}，请求参数{}，返回内容：{}", JSONObject.toJSONString(path), body, result);
            }
        } else {
            log.info("请求根据人员唯一字段获取人员详细信息失败，返回无结果，请求地址：{}，请求参数{}", JSONObject.toJSONString(path), body);
        }
        return null;
    }

    /**
     * 添加作业人员活填充人员编号
     *
     * @param newWorkPersonnel
     * @param personnelInfoVO
     * @return void
     * @author ljx
     * @time 2024/10/24 14:58
     */
    private void insertOrUpdateWorkPersonnel(WorkPersonnel newWorkPersonnel, PersonnelInfoVO personnelInfoVO) {
        boolean flag = false;
        String idCardNo = newWorkPersonnel.getIdCardNo();
        String extEventPersonNo = newWorkPersonnel.getExtEventPersonNo();
        //根据身份证号查询是否有这个人
        Personnel personnel = personnelMapper.selectOne(Wrappers.<Personnel>lambdaQuery().eq(Personnel::getIdCardNo, idCardNo));
        WorkPersonnel workPersonnel = workPersonnelMapper.selectOne(Wrappers.<WorkPersonnel>lambdaQuery().eq(WorkPersonnel::getIdCardNo, idCardNo));

        //新加作业人员
        if (personnel == null && workPersonnel == null) {
            //给人员信息生成一个唯一工号
            newWorkPersonnel.setTemporaryEmpNo(IdUtil.objectId());
            //根据身份证号计算年龄
            String year = idCardNo.substring(6, 10);
            String month = idCardNo.substring(10, 12);
            String day = idCardNo.substring(12, 14);
            String dateOfBirth = year + "-" + month + "-" + day + " 00:00:00";
            newWorkPersonnel.setAge(DateUtils.getAgeByDate(dateOfBirth));
            flag = workPersonnelMapper.insert(newWorkPersonnel) > NumericalConstants.ZERO;
            if (flag) {
                workPersonnelCached.setWorkPersonnelInfo(newWorkPersonnel);
                log.info("人脸识别同步作业人员信息成功，人员数据：{}", newWorkPersonnel);
            } else {
                log.info("人脸识别同步作业人员信息失败，人员数据：{}", newWorkPersonnel);
            }
        } else {
            //填充人员编号进数据库，但不填充进缓存
            if (personnel != null) {
                flag = personnelMapper.updateById(new Personnel(personnel.getPersonnelId(), extEventPersonNo)) > NumericalConstants.ZERO;
                if (flag) {
                    personnelInfoVO.setPersonnel(personnel);
                    log.info("人脸识别修改负责人信息成功，人员数据：{}", newWorkPersonnel);
                } else {
                    log.info("人脸识别修改负责人信息失败，人员数据：{}", newWorkPersonnel);
                }
            } else {
                flag = workPersonnelMapper.updateById(new WorkPersonnel(workPersonnel.getWorkPersonnelId(), extEventPersonNo)) > NumericalConstants.ZERO;
                if (flag) {
                    personnelInfoVO.setWorkPersonnel(workPersonnel);
                    log.info("人脸识别修改作业人员信息成功，人员数据：{}", newWorkPersonnel);
                } else {
                    log.info("人脸识别修改作业人员信息成功，人员数据：{}", newWorkPersonnel);
                }
            }
        }
    }

    /**
     * 设备状态信息上报处理
     *
     * @param acData
     * @return void
     * @author ljx
     * @time 2024/9/24 11:56
     */
//    private void acDataHandle(ACData acData) {
//        ACData oldAcData = BindCardMachineCache.getACDeviceStatusData(acData.getDeviceId());
//        if (oldAcData != null) {
//            ACData newAcData = new ACData();
//            BeanUtils.copyProperties(acData, newAcData);
//            //仓位信息覆盖先旧后新
//            Map<Integer, StockInfo> stockInfoMap = new HashMap<>();
//            oldAcData.getStockInfos().forEach(stockInfo -> stockInfoMap.put(stockInfo.getSerialNum(), stockInfo));
//            acData.getStockInfos().forEach(stockInfo -> stockInfoMap.put(stockInfo.getSerialNum(), stockInfo));
//            acData.setStockInfos(stockInfoMap.values().stream().sorted(Comparator.comparing(StockInfo::getSerialNum)).collect(Collectors.toList()));
//        }
//        BindCardMachineCache.putAcDeviceStatusData(acData);
//    }

}
